package com.wosai.medical.hospital.demo.controller.patient;

import com.google.common.collect.Lists;
import com.wosai.medical.hospital.demo.common.DataResult;
import com.wosai.medical.hospital.demo.controller.BaseController;

import com.wosai.medical.hospital.demo.controller.patient.response.FeeOrder;
import com.wosai.medical.hospital.demo.controller.patient.response.OrderPay;
import com.wosai.medical.hospital.demo.controller.patient.response.PatientQueue;
import com.wosai.medical.hospital.demo.controller.patient.response.PendFees;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc
 * @auther 韦湘平
 * @date 2020/2/20 11:15 上午
 */

@Slf4j
@Api(tags = "就诊人")
@RestController
@RequestMapping("/hospital")
public class PatientController extends BaseController {


    /**
     * 3.1. 候诊排队查询
     *
     * @param apiAppId    接入账号
     * @param apiVersion  接口版本号
     * @param apiStamp    时间戳
     * @param apiSign     参数签名
     * @param patientCode 就诊卡类型(11-身份证,21-健康卡,31-诊疗卡)
     * @return 候诊信息
     */
    @ApiOperation("3.1. 候诊排队查询")
    @GetMapping("/patient/{patientCode}/clinic/queue")
    public RespResult<DataResult> getPatientQueue(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            //患者代码(HIS患者唯一标识)
            @PathVariable final String patientCode
    ) {
        return action(new Process<DataResult>() {
                          @Override
                          public String getAppId() {
                              return apiAppId;
                          }

                          @Override
                          public Integer getVersion() {
                              return apiVersion;
                          }

                          @Override
                          public Long getStamp() {
                              return apiStamp;
                          }

                          @Override
                          public String getSign() {
                              return apiSign;
                          }

                          @Override
                          public Map<String, Serializable> getParams() {
                              return new HashMap<String, Serializable>(2) {
                                  {
                                      put("patientCode", patientCode);
                                  }
                              };
                          }

                          @Override
                          public DataResult handler() {
                              ///TODO: 业务处理
                              List<Serializable> PatientQueuea = Lists.newArrayList(
                                      PatientQueue.builder()
                                              .dept(PatientQueue.Dept.builder().code("12345").name("测试科室").location("科室位置").build())
                                              .doctor(PatientQueue.Doctor.builder().code("12331").name("刘德华").build())
                                              .serialNum(10)
                                              .currentNum(5)
                                              .frontNum(4)
                                              .visitTime("2020-03-08 01:00:00")
                                              .build()
                              );
                              return DataResult.builder().totals(1L).rows(PatientQueuea).build();
                          }
                      }
        );
    }

    /**
     * 4.1. 门诊待缴费记录
     *
     * @param apiAppId    接入账号
     * @param apiVersion  接口版本号
     * @param apiStamp    时间戳
     * @param apiSign     参数签名
     * @param patientCode 就诊卡类型(11-身份证,21-健康卡,31-诊疗卡)
     * @return 候诊信息
     */
    @ApiOperation("4.1. 门诊待缴费记录")
    @GetMapping("/patient/{patientCode}/clinic/pend/fees")
    public RespResult<DataResult> getPendFee(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            //患者代码(HIS患者唯一标识)
            @PathVariable final String patientCode
    ) {
        return action(new Process<DataResult>() {
                          @Override
                          public String getAppId() {
                              return apiAppId;
                          }

                          @Override
                          public Integer getVersion() {
                              return apiVersion;
                          }

                          @Override
                          public Long getStamp() {
                              return apiStamp;
                          }

                          @Override
                          public String getSign() {
                              return apiSign;
                          }

                          @Override
                          public Map<String, Serializable> getParams() {
                              return new HashMap<String, Serializable>(2) {
                                  {
                                      //就诊卡类型(11-身份证,21-健康卡,31-诊疗卡)
                                      put("patientCode", patientCode);
                                  }
                              };
                          }

                          @Override
                          public DataResult handler() {
                              ///TODO: 业务处理
                              List<Serializable> pendFees = Lists.newArrayList(
//                                      PendFees.builder()
//                                              .hisOrderCode("1sss33333")
//                                              .canPay(true)
//                                              .limitPayRemark("yes")
//                                              .createTime("2020-03-02 01:00:00")
//                                              .dept(PendFees.Dept.builder().code("12345").name("测试科室").build())
//                                              .doctor(PendFees.Doctor.builder().code("12331").name("刘德华").build())
//                                              .totalAmount(1000L)
//                                              .payAmount(9000L)
//                                              .build()


                              );
                              return DataResult.builder().totals(0L).rows(pendFees).build();
                          }
                      }
        );
    }

    /**
     * 4.2. 门诊待缴费明细
     *
     * @param apiAppId     接入账号
     * @param apiVersion   接口版本号
     * @param apiStamp     时间戳
     * @param apiSign      参数签名
     * @param hisOrderCode 门诊流水号
     * @return 获取患者信息
     */
    @ApiOperation("4.2. 门诊待缴费明细")
    @GetMapping("/clinic/pend/fee/order/{hisOrderCode}")
    public RespResult<FeeOrder> getFeeDetail(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            //门诊流水号(缴费唯一凭证,用于后续结算)
            @PathVariable final String hisOrderCode
    ) {
        return action(new Process<FeeOrder>() {
            @Override
            public String getAppId() {
                return apiAppId;
            }

            @Override
            public Integer getVersion() {
                return apiVersion;
            }

            @Override
            public Long getStamp() {
                return apiStamp;
            }

            @Override
            public String getSign() {
                return apiSign;
            }

            @Override
            public Map<String, Serializable> getParams() {
                return new HashMap<String, Serializable>(2) {
                    {
                        put("hisOrderCode", hisOrderCode);
                    }
                };
            }

            @Override
            public FeeOrder handler() {
                ///TODO: 业务处理
                return FeeOrder.builder()
                        .prompt("注意安全")
                        .createTime("2020-03-02 01:00:00")
                        .dept(FeeOrder.Dept.builder().code("12345").name("测试科室").build())
                        .doctor(FeeOrder.Doctor.builder().code("12331").name("刘德华").build())
                        .patient(FeeOrder.Patient.builder().code("a122").name("阿点点滴滴").build())
                        .totalAmount(10000L)
                        .payAmount(9000L)
                        .recipes(Lists.newArrayList(
                                FeeOrder.Recipe.builder()
                                        .code("ddssa")
                                        .type("感冒")
                                        .fee(1000L)
                                        .items(Lists.newArrayList(
                                                FeeOrder.Item.builder()
                                                        .code("A1234")
                                                        .name("皮酥昂")
                                                        .uint("个")
                                                        .price(100L)
                                                        .spec("1个/天")
                                                        .quantity(10)
                                                        .totalFee(1000L)
                                                        .build()
                                        ))
                                        .build()
                        ))
                        .build();
            }
        });
    }

    /**
     * 4.3. 门诊缴费支付
     *
     * @param apiAppId   接入账号
     * @param apiVersion 接口版本号
     * @param apiStamp   时间戳
     * @param apiSign    参数签名
     * @param req        请求参数
     * @return 获取患者信息
     */
    @ApiOperation("4.3. 门诊缴费支付")
    @PostMapping("/clinic/fee/order/pay")
    public RespResult<OrderPay> orderPay(
            //接入账号(app_id)
            @RequestHeader("api_app_id") final String apiAppId,
            //接口版本号(采用正整数描述)
            @RequestHeader("api_version") final Integer apiVersion,
            //时间戳(13位到毫秒)
            @RequestHeader("api_stamp") final Long apiStamp,
            //参数签名
            @RequestHeader("api_sign") final String apiSign,
            @RequestBody(required = false) final Map<String, Serializable> req
    ) {
        return action(new Process<OrderPay>() {
            @Override
            public String getAppId() {
                return apiAppId;
            }

            @Override
            public Integer getVersion() {
                return apiVersion;
            }

            @Override
            public Long getStamp() {
                return apiStamp;
            }

            @Override
            public String getSign() {
                return apiSign;
            }

            @Override
            public Map<String, Serializable> getParams() {
                return req;
            }

            @Override
            public OrderPay handler() {
                ///TODO: 业务处理
                return OrderPay.builder()
                        .hisOrderCode("2233333")
                        .receiptNo("A12334")
                        .payCertificate("sdfsdfdsf")
                        .settleTime("2020-03-02 11:00:01")
                        .build();
            }
        });
    }
}
